﻿using AForge.Video.DirectShow;
using System;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;

namespace ArcSoftFace
{
    /// <summary>
    /// 暂时只开启了RGB摄像头
    /// </summary>
    public partial class FaceForm2 : Form
    {
        /// <summary>
        /// 视频输入设备信息
        /// </summary>
        private FilterInfoCollection filterInfoCollection;
        /// <summary>
        /// RGB摄像头设备
        /// </summary>
        private VideoCaptureDevice rgbDeviceVideo;
        /// <summary>
        /// IR摄像头设备
        /// </summary>
        private VideoCaptureDevice irDeviceVideo;
        /// <summary>
        /// RGB 摄像头索引
        /// </summary>
        private int rgbCameraIndex = 0;
        /// <summary>
        /// IR 摄像头索引
        /// </summary>
        private int irCameraIndex = 0;
        /// <summary>
        /// 是否是双目摄像
        /// </summary>
        private bool isDoubleShot = false;

        public FaceForm2()
        {
            InitializeComponent();

            this.rgbVideoSource.Paint += new System.Windows.Forms.PaintEventHandler(this.videoSource_Paint);

            CheckForIllegalCrossThreadCalls = false;
            //初始化引擎
            //InitEngines();
            lblMessage.Text = FaceCompare.InitEngines();
            if (lblMessage.Text != "1")
            {
                return;
            }
            //隐藏摄像头图像窗口
            rgbVideoSource.Hide();
            irVideoSource.Hide();

            //从配置文件读取摄像头序号
            AppSettingsReader reader = new AppSettingsReader();
            rgbCameraIndex = (int)reader.GetValue("RGB_CAMERA_INDEX", typeof(int));
            irCameraIndex = (int)reader.GetValue("IR_CAMERA_INDEX", typeof(int));

            //注册一张人脸到人脸库,如果要注册多张循环调用就可以了
            Image image = Image.FromFile(@"d:\zp.jpg");
            pictureBox1.Image = image;
            FaceCompare.RegisterImage(image);

        }

        /// <summary>
        /// RGB摄像头Paint事件，图像显示到窗体上，得到每一帧图像，并进行处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoSource_Paint(object sender, PaintEventArgs e)
        {
            if (rgbVideoSource.IsRunning)
            {
                //得到当前RGB摄像头下的图片
                Bitmap bitmap = rgbVideoSource.GetCurrentVideoFrame();
                lblMessage.Text = FaceCompare.DoRgbCompare(bitmap, rgbVideoSource.Width, rgbVideoSource.Height);
            }
        }

        /// <summary>
        /// 摄像头按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartVideo_Click(object sender, EventArgs e)
        {
            //在点击开始的时候再坐下初始化检测，防止程序启动时有摄像头，在点击摄像头按钮之前将摄像头拔掉的情况
            initVideo();
            //必须保证有可用摄像头
            if (filterInfoCollection.Count == 0)
            {
                MessageBox.Show("未检测到摄像头，请确保已安装摄像头或驱动!");
                return;
            }
            if (rgbVideoSource.IsRunning || irVideoSource.IsRunning)
            {
                btnStartVideo.Text = "启用摄像头";
                //关闭摄像头
                if (irVideoSource.IsRunning)
                {
                    irVideoSource.SignalToStop();
                    irVideoSource.Hide();
                }
                if (rgbVideoSource.IsRunning)
                {
                    rgbVideoSource.SignalToStop();
                    rgbVideoSource.Hide();
                }
            }
            else
            {
                //“选择识别图”、“开始匹配”按钮禁用，阈值控件可用，显示摄像头控件
                rgbVideoSource.Show();
                irVideoSource.Show();
                btnStartVideo.Text = "关闭摄像头";
                //获取filterInfoCollection的总数
                int maxCameraCount = filterInfoCollection.Count;
                //如果配置了两个不同的摄像头索引
                if (rgbCameraIndex != irCameraIndex && maxCameraCount >= 2)
                {
                    //RGB摄像头加载
                    rgbDeviceVideo = new VideoCaptureDevice(filterInfoCollection[rgbCameraIndex < maxCameraCount ? rgbCameraIndex : 0].MonikerString);
                    rgbDeviceVideo.VideoResolution = rgbDeviceVideo.VideoCapabilities[0];
                    rgbVideoSource.VideoSource = rgbDeviceVideo;
                    rgbVideoSource.Start();

                    //IR摄像头
                    irDeviceVideo = new VideoCaptureDevice(filterInfoCollection[irCameraIndex < maxCameraCount ? irCameraIndex : 0].MonikerString);
                    irDeviceVideo.VideoResolution = irDeviceVideo.VideoCapabilities[0];
                    irVideoSource.VideoSource = irDeviceVideo;
                    irVideoSource.Start();
                    //双摄标志设为true
                    isDoubleShot = true;
                }
                else
                {
                    //仅打开RGB摄像头，IR摄像头控件隐藏
                    rgbDeviceVideo = new VideoCaptureDevice(filterInfoCollection[rgbCameraIndex <= maxCameraCount ? rgbCameraIndex : 0].MonikerString);
                    rgbDeviceVideo.VideoResolution = rgbDeviceVideo.VideoCapabilities[0];
                    rgbVideoSource.VideoSource = rgbDeviceVideo;
                    rgbVideoSource.Start();
                    irVideoSource.Hide();
                }
            }
        }

        /// <summary>
        /// 摄像头初始化
        /// </summary>
        private void initVideo()
        {
            filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            //如果没有可用摄像头，“启用摄像头”按钮禁用，否则使可用
            if (filterInfoCollection.Count == 0)
            {
                lblMsg.Text = "没有摄像头";
                // btnStartVideo.Enabled = false;
            }
            else
            {
                lblMsg.Text = "已初始化摄像头";
                // btnStartVideo.Enabled = true;
            }
        }
    }
}
